import sys
import os
import uuid
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from PySide6.QtXml import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.xml=os.path.join(self.parent.getAppDir(),'test.xml')
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)        
        
        toolBar=QToolBar()
        btn=QPushButton('Create')
        btn.clicked.connect(self.Create)
        toolBar.addWidget(btn)

        btn=QPushButton('Add Root')
        btn.clicked.connect(self.AddRoot)
        toolBar.addWidget(btn)

        btn=QPushButton('Add Child')
        btn.clicked.connect(self.AddChild)
        toolBar.addWidget(btn)

        btn=QPushButton('Add Child with attribute')
        btn.clicked.connect(self.AddChildAttribute)
        toolBar.addWidget(btn)

        btn=QPushButton('Add Child with TextNode')
        btn.clicked.connect(self.AddChildTextNode)
        toolBar.addWidget(btn)

        btn=QPushButton('SaveFile')
        btn.clicked.connect(self.SaveFile)
        toolBar.addWidget(btn)

        btn=QPushButton('LoadFile')
        btn.clicked.connect(self.LoadFile)
        toolBar.addWidget(btn)
        layout.addWidget(toolBar)

        self.text=QTextBrowser()#QTextEdit的只读控件
        layout.addWidget(self.text)

        self.setLayout(layout)    
    def Create(self):        
        self.doc= QDomDocument()
        self.doc.appendChild(self.doc.createProcessingInstruction('xml',"version=\"1.0\" encoding=\"UTF-8\""))
        self.text.setPlainText(self.doc.toString())
    def AddRoot(self):
        if self.root is None:
            self.root=self.doc.createElement('Books')
            self.doc.appendChild(self.root)
            self.text.setPlainText(self.doc.toString())
    def AddChild(self):        
        child=self.doc.createElement('Book')        
        self.root.appendChild(child)
        self.text.setPlainText(self.doc.toString())
    def AddChildAttribute(self):
        child=self.doc.createElement('Book')
        child.setAttribute('ID',uuid.uuid1().hex)
        child.setAttribute('Name',f'BookName_{random.randint(1,100)}')
        child.setAttribute('Price',f'{random.randint(1,100)}')
        self.root.appendChild(child)
        self.text.setPlainText(self.doc.toString())
    def AddChildTextNode(self):
        child=self.doc.createElement('Book')
        child.setAttribute('ID',uuid.uuid1().hex)
        child.setAttribute('Name',f'BookName_{random.randint(1,100)}')
        child.setAttribute('Price',f'{random.randint(1,100)}')
        des=self.doc.createElement('Description')
        des.appendChild(self.doc.createTextNode('Book Description'))
        child.appendChild(des)
        self.root.appendChild(child)
        self.text.setPlainText(self.doc.toString())
    def SaveFile(self):
        file=QFile(self.xml)
        file.open(QIODeviceBase.OpenModeFlag.ReadWrite|QIODeviceBase.OpenModeFlag.Truncate)
        self.doc.save(QTextStream(file),4)
        file.close()
    def LoadFile(self):
        file=QFile(self.xml)
        file.open(QIODeviceBase.OpenModeFlag.ReadOnly)
        content=file.readAll().toStdString()       
        self.text.setPlainText(content)

        self.doc= QDomDocument()
        #self.doc.setContent(file)#此方式无效，暂时不清楚原因
        self.doc.setContent(content)
        file.close()
        self.root=self.doc.documentElement()        
        
        
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet